package com.example.exchangeapp.yy.fragment;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Looper;
import android.os.ParcelUuid;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.exchangeapp.R;
import com.example.exchangeapp.yy.constant.UserLogin;
import com.example.exchangeapp.yy.entity.GoodVo2;
import com.example.exchangeapp.yy.entity.Goods;
import com.example.exchangeapp.yy.entity.GoodsVo;
import com.example.exchangeapp.yy.fragment.HasSendGoods;
import com.example.exchangeapp.yy.request.HttpRequestUtils;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.logging.Handler;

public class dialog_goods_details extends Dialog {
     public static GoodVo2 goodVo2 = null;
     private ImageView imageView=null;


    public dialog_goods_details(@NonNull Context context,GoodVo2 goodVo2,int position) {
        super(context);
        setContentView(R.layout.activity_dialog_goods_details);
        TextView type = (TextView)findViewById(R.id.gtype);
        TextView price = (TextView)findViewById(R.id.price);
        TextView content = (TextView)findViewById(R.id.content);
        TextView addr = (TextView)findViewById(R.id.addr);
        imageView = (ImageView)findViewById(R.id.img);
        String imgPath = goodVo2.getImageUrlList()[0];
        System.out.println("点击的商品信息："+goodVo2);
        URL url = null;
        try {
            url = new URL(imgPath);
            requestImg(url);
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }

        type.setText(goodVo2.getTypeName());
        price.setText(goodVo2.getPrice().toString());
        content.setText(goodVo2.getContent());
        addr.setText(goodVo2.getAddr());

        findViewById(R.id.ret).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                HasSendGoods.mDialog.dismiss();
            }
        });

        findViewById(R.id.delete).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                System.out.println("用户id:"+UserLogin.uid+"商品id:"+dialog_goods_details.goodVo2.id);
                HttpRequestUtils.delete(UserLogin.uid+"",HasSendGoods.data.get(position).getId());
                 while (HasSendGoods.code!=200){

                 }
                 if (position<HasSendGoods.data.size()){
                     HasSendGoods.data.remove(position);
                 }

                HasSendGoods.mDialog.dismiss();

                    HasSendGoods.code=0;
                Toast.makeText(getContext(), "下架成功!", Toast.LENGTH_LONG).show();
                HasSendGoods.mAdapter.notifyDataSetChanged();
                try {
                    Thread.sleep(200);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                //重新渲染数据
                HasSendGoods.mAdapter.notifyDataSetChanged();
            }
        });


    }

    private void requestImg(final URL imgUrl)
    {
        new Thread(new Runnable() {
            @Override
            public void run() {
                Bitmap bitmap = null;
                try {
                    bitmap = BitmapFactory.decodeStream(imgUrl.openStream());
                    showImg(bitmap);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }
    private void showImg(final Bitmap bitmap){
        imageView.setImageBitmap(bitmap);
    }






    @Override
    public void show() {
        super.show();
    }

    @Nullable
    @Override
    public Window getWindow() {
        return super.getWindow();
    }

    @Override
    public void cancel() {
        super.cancel();
    }
}